package com.j2eefast.bussiness.contract.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.bussiness.contract.entity.ContractEntity;
import com.j2eefast.bussiness.contract.mapper.ContractMapper;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * <p></p>
 *
 * @author: guoshenghui
 * @date: 2021-05-30 00:02
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
public class ContractService extends ServiceImpl<ContractMapper, ContractEntity> {

    @Resource
    protected ContractMapper contractMapper;

    public PageUtil contractList(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String title2 = (String) params.get("title2");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String contractSn2=(String)params.get("contractSn2");
        String saleman=(String)params.get("saleman");
        String company=(String)params.get("company");
        String beginTime=(String)params.get("beginTime");
        String endTime=(String)params.get("endTime");
        String payType=(String)params.get("payType");
        String type=(String)params.get("type");
        String status=(String)params.get("status");

        Page<ContractEntity> page = contractMapper.contractList(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(title2,""),
                StrUtil.nullToDefault(contractSn,""),
                StrUtil.nullToDefault(contractSn2,""),
                StrUtil.nullToDefault(saleman,""),
                StrUtil.nullToDefault(company,""),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(payType,""),
                StrUtil.nullToDefault(type,""),
                StrUtil.nullToDefault(status,""),
                StrUtil.nullToDefault(endTime,""));
        return new PageUtil(page);
    }

    public List<ContractEntity> findContract(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String title2 = (String) params.get("title2");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String contractSn2=(String)params.get("contractSn2");
        String saleman=(String)params.get("saleman");
        String company=(String)params.get("company");
        String beginTime=(String)params.get("beginTime");
        String endTime=(String)params.get("endTime");
        String payType=(String)params.get("payType");
        String type=(String)params.get("type");
        String status=(String)params.get("status");
        List<ContractEntity> page = contractMapper.findContract(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(contractSn,"")
                ,StrUtil.nullToDefault(payType,"")
                ,StrUtil.nullToDefault(type,"")
                ,StrUtil.nullToDefault(status,"")
                ,StrUtil.nullToDefault(company,"")
                ,StrUtil.nullToDefault(beginTime,"")
                ,StrUtil.nullToDefault(title2,"")
                ,StrUtil.nullToDefault(endTime,"")
                ,StrUtil.nullToDefault(contractSn2,"")
                ,StrUtil.nullToDefault(saleman,""));
        return page;
    }


    public PageUtil contractList2(Map<String, Object> params) {
        Integer userId= Integer.parseInt(UserUtils.getUserInfo().getId().toString());
        String title = (String) params.get("title");	//文件名称
        String title2 = (String) params.get("title2");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String contractSn2=(String)params.get("contractSn2");
        String payType=(String)params.get("payType");
        String type=(String)params.get("type");
        String company=(String)params.get("company");
        String status=(String)params.get("status");
        String saleman=(String)params.get("saleman");
        String beginTime=(String)params.get("beginTime");
        String endTime=(String)params.get("endTime");
        Page<ContractEntity> page = contractMapper.contractList2(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(contractSn,"")
                ,StrUtil.nullToDefault(payType,"")
                ,StrUtil.nullToDefault(type,"")
                ,StrUtil.nullToDefault(status,"")
                ,StrUtil.nullToDefault(saleman,""),
                StrUtil.nullToDefault(title2,""),
                StrUtil.nullToDefault(contractSn2,""),
                StrUtil.nullToDefault(company,""),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(endTime,""),
                userId);
        return new PageUtil(page);
    }
    public List<ContractEntity> findMyContract(Map<String, Object> params) {
        Integer userId= Integer.parseInt(UserUtils.getUserInfo().getId().toString());
        String title = (String) params.get("title");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String payType=(String)params.get("payType");
        String type=(String)params.get("type");
        String status=(String)params.get("status");
        String saleman=(String)params.get("saleman");
        List<ContractEntity> page = contractMapper.findMyContract(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(contractSn,"")
                ,StrUtil.nullToDefault(payType,"")
                ,StrUtil.nullToDefault(type,"")
                ,StrUtil.nullToDefault(status,"")
                ,StrUtil.nullToDefault(saleman,""),
                userId);
        return page;
    }


//    public PageUtil contractList4(Map<String, Object> params) {
//        String title = (String) params.get("title");	//文件名称
//        String contractSn=(String)params.get("contractSn");
//        String payType=(String)params.get("payType");
//        String type=(String)params.get("type");
//        String saleman=(String)params.get("saleman");
//        Page<ContractEntity> page = contractMapper.contractList4(new Query<ContractEntity>(params).getPage(),
//                StrUtil.nullToDefault(title,""),
//                StrUtil.nullToDefault(contractSn,"")
//                ,StrUtil.nullToDefault(payType,"")
//                ,StrUtil.nullToDefault(type,"")
//                ,StrUtil.nullToDefault(saleman,""));
//        return new PageUtil(page);
//    }
    public PageUtil contractList4(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String title2 = (String) params.get("title2");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String contractSn2=(String)params.get("contractSn2");
        String saleman=(String)params.get("saleman");
        String company=(String)params.get("company");
        String beginTime=(String)params.get("beginTime");
        String endTime=(String)params.get("endTime");
        String type=(String)params.get("type");
        String payType=(String)params.get("payType");
        Page<ContractEntity> page = contractMapper.contractList4(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(title2,""),
                StrUtil.nullToDefault(contractSn,""),
                StrUtil.nullToDefault(contractSn2,""),
                StrUtil.nullToDefault(saleman,""),
                StrUtil.nullToDefault(company,""),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(type,""),
                StrUtil.nullToDefault(payType,""),
                StrUtil.nullToDefault(endTime,""));
        return new PageUtil(page);
    }

    public  List<ContractEntity> findRiskContract(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String payType=(String)params.get("payType");
        String type=(String)params.get("type");
        String saleman=(String)params.get("saleman");
        List<ContractEntity> page = contractMapper.findRiskContract(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(contractSn,"")
                ,StrUtil.nullToDefault(payType,"")
                ,StrUtil.nullToDefault(type,"")
                ,StrUtil.nullToDefault(saleman,""));
        return page;
    }


    public PageUtil contractList3(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String title2 = (String) params.get("title2");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String contractSn2=(String)params.get("contractSn2");
        String saleman=(String)params.get("saleman");
        String company=(String)params.get("company");
        String beginTime=(String)params.get("beginTime");
        String endTime=(String)params.get("endTime");
        String type=(String)params.get("type");
        Page<ContractEntity> page = contractMapper.contractList3(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(title2,""),
                StrUtil.nullToDefault(contractSn,""),
                StrUtil.nullToDefault(contractSn2,""),
                StrUtil.nullToDefault(saleman,""),
                StrUtil.nullToDefault(company,""),
                StrUtil.nullToDefault(beginTime,""),
                StrUtil.nullToDefault(type,""),
                StrUtil.nullToDefault(endTime,""));
        return new PageUtil(page);
    }


    public List<ContractEntity> findVoidContract(Map<String, Object> params) {
        String title = (String) params.get("title");	//文件名称
        String contractSn=(String)params.get("contractSn");
        String type=(String)params.get("type");
        String saleman=(String)params.get("saleman");
        List<ContractEntity> page = contractMapper.findVoidContract(new Query<ContractEntity>(params).getPage(),
                StrUtil.nullToDefault(title,""),
                StrUtil.nullToDefault(contractSn,"")
                ,StrUtil.nullToDefault(type,"")
                ,StrUtil.nullToDefault(saleman,"")
        );
        return page;
    }






    public boolean save(ContractEntity contractEntity){
        String code=contractSnCode(contractEntity.getSignTime());
        contractEntity.setContractSn(code);
        return contractMapper.save(contractEntity);
    }

    public boolean deleteContract(Long[] ids ){
        return contractMapper.deleteContract(ids);
    }

    public boolean doDelete( Long id){
        return contractMapper.doDelete(id);
    }

    /**
     * 合同编号编码（按照日期）
     * @param s
     * @return
     */
    public String contractSnCode(Date s){
        SimpleDateFormat sdf = new SimpleDateFormat("(yyyy)MM-DD-ss");
        String code=sdf.format(s);
        return code;
    }


    public ContractEntity getContractById(Long id){
        return contractMapper.getContractById( id);
    }


    public boolean updateByIds(ContractEntity contractEntity){
        return contractMapper.updateByIds( contractEntity);
    }

    public  boolean doCancel(Long id){
        return contractMapper.doCancel(id);
    }



}
